package com.ironone.mediaplayerdemo;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Handler;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ToggleButton;

public class MainActivity extends Activity {
	//public static MediaPlayer mediaPlayer; //
	ImageView loading;
	Animation rotation;
	ToggleButton playButton;
	ToggleButton playHDButton;
	TextView textViewLoading;
	Button nextButton;

	//boolean isError = false;
	//int isError_ErrorCode = 0;
	//NotificationManager mNotificationManager;
	BroadcastReceiver br_commonReceiver;
	int timerToStopPlayback = 5000; // In milliseconds. After this time paused player will be reseted.  
	private static Handler handler = new Handler();
	Runnable runnable;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		textViewLoading = (TextView) findViewById(R.id.textViewLoading);
		playButton = (ToggleButton) findViewById(R.id.togglebutton);
		playHDButton = (ToggleButton) findViewById(R.id.togglehdbutton);
		nextButton = (Button) findViewById(R.id.next_button);
		loading = (ImageView) findViewById(R.id.loading);

		rotation = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.rotate_button);
		rotation.setRepeatCount(Animation.INFINITE);

		//mediaPlayer = MyMediaPlayer.getMediaPlayerInstance();
		//mediaPlayer.setWakeMode(getApplicationContext(), PowerManager.PARTIAL_WAKE_LOCK);

		/*if(MyMediaPlayer.getMediaPlayerStatus() == MediaPlayerStatus.PLAYING ||
				MyMediaPlayer.getMediaPlayerInstance().isPlaying()) {
			playButton.setChecked(true);
		}
		if(MyMediaPlayer.getIsHDOn()){
			playHDButton.setChecked(true);
		}*/

		Intent serviceIntent = new Intent(getApplicationContext(), MyMediaPlayerService.class);
		startService(serviceIntent);

		/*mediaPlayer.setOnPreparedListener(new OnPreparedListener() {

			public void onPrepared(MediaPlayer mp) {
				try {
					stopLoading();
					if(MyMediaPlayer.getMediaPlayerStatus() != MediaPlayerStatus.FORCE_PAUSED){
						textViewLoading.setText("playing...");
						MyMediaPlayer.playMediaPlayer();						
					}
				}catch (Exception e) {
					playButton.setChecked(false);
					MyMediaPlayer.releaseMediaPlayer();
				}
			}
		});*/

		/*mediaPlayer.setOnCompletionListener(new OnCompletionListener() {

			@Override
			public void onCompletion(MediaPlayer arg0) {
				textViewLoading.setText("on complete");

				if(isError){
					textViewLoading.setText("on complete - error");
					stopLoading();
					MyMediaPlayer.resetMediaPlayer();

					switch(isError_ErrorCode){
					case -1004:
						Toast.makeText(getBaseContext(), "MEDIA_ERROR_IO", Toast.LENGTH_LONG).show();
						break;
					case -1007:
						Toast.makeText(getBaseContext(), "MEDIA_ERROR_MALFORMED", Toast.LENGTH_LONG).show();
						break;
					case 200:
						Toast.makeText(getBaseContext(), "MEDIA_ERROR_NOT_VALID_FOR_PROGRESSIVE_PLAYBACK", Toast.LENGTH_LONG).show();
						break;
					case 100:
						Toast.makeText(getBaseContext(), "MEDIA_ERROR_SERVER_DIED", Toast.LENGTH_LONG).show();
						break;
					case -110:
						Toast.makeText(getBaseContext(), "MEDIA_ERROR_TIMED_OUT", Toast.LENGTH_LONG).show();
						break;
					case 1:
						Toast.makeText(getBaseContext(), "MEDIA_ERROR_UNKNOWN", Toast.LENGTH_LONG).show();
						break;
					case -1010:
						Toast.makeText(getBaseContext(), "MEDIA_ERROR_UNSUPPORTED", Toast.LENGTH_LONG).show();
						break;
					}
					playButton.setChecked(false);
				}else {
					startLoading();
					MyMediaPlayer.resetAndPrepareMediaPlayer();		
				}
			}
		});*/

		/*mediaPlayer.setOnBufferingUpdateListener(new OnBufferingUpdateListener() {

			@Override
			public void onBufferingUpdate(MediaPlayer arg0, int percent) {
				//textViewLoading.setText(percent + "%");
				if (percent == 100) {
					textViewLoading.setText("buffer 100");
					startLoading();
					MyMediaPlayer.resetAndPrepareMediaPlayer();
				}
			}
		});*/

		/*mediaPlayer.setOnInfoListener(new OnInfoListener() {

			@Override
			public boolean onInfo(MediaPlayer mp, int what, int extra) {

				if (what == MediaPlayer.MEDIA_INFO_BUFFERING_START || what == 703) {
					startLoading();
					textViewLoading.setText("bufferring");
					MyMediaPlayer.setMediaPlayerStatus(MediaPlayerStatus.BUFFERING);
				} else if (what == MediaPlayer.MEDIA_INFO_BUFFERING_END) {		
					textViewLoading.setText("playing...");
					stopLoading();
				}else {
					stopLoading();
				}
				return false;
			}
		});*/

		/*mediaPlayer.setOnErrorListener(new OnErrorListener() {

			@Override
			public boolean onError(MediaPlayer mp, int what, int extra) {
				isError = true;
				isError_ErrorCode = extra;
				return false;
			}
		});*/

		nextButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent i = new Intent(MainActivity.this, SecondActivity.class);
				startActivity(i);
				overridePendingTransition(R.anim.slide_left_to_middle, R.anim.slide_middle_to_right);
				finish();
			}
		});

		nextButton.setOnTouchListener(new OnTouchListener() {
			/*@Override
		    public boolean onTouch(View v, MotionEvent event) {
		        if(event.getAction() == MotionEvent.ACTION_UP){

		            // Do what you want
		            return true;
		        }
		        return false;
		    }*/

			@Override
			public boolean onTouch(View v, MotionEvent event) {

				if(event.getAction() == MotionEvent.ACTION_MOVE){
					Intent i = new Intent(MainActivity.this, SecondActivity.class);
					startActivity(i);
					overridePendingTransition(R.anim.slide_left_to_middle, R.anim.slide_middle_to_right);
					finish();
				}
				return false;
			}
		});
	}

	@Override
	protected void onPause() {
		try {
			super.onPause();
			startNotificationOnStatusBar();
			MyMediaPlayer.setIsPlayerOnBackground(true);
			unregisterReceiver(br_commonReceiver);
		} catch (Exception e) {
			//stopLoading();
			//playButton.setChecked(false);
			//MyMediaPlayer.releaseMediaPlayer();
		}
	}

	@Override
	protected void onResume() {
		try {
			super.onResume();

			//mediaPlayer = MyMediaPlayer.getMediaPlayerInstance();
			MyMediaPlayer.setIsPlayerOnBackground(false);

			rotation = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.rotate_button);
			rotation.setRepeatCount(Animation.INFINITE);

			myMediaPlayerServiceCallbacks();
			stopNotificationOnStatusBar();

			if(MyMediaPlayer.getMediaPlayerStatus() == MediaPlayerStatus.PLAYING ||
					MyMediaPlayer.getMediaPlayerInstance().isPlaying()) {
				playButton.setChecked(true);
			}
			if(MyMediaPlayer.getIsHDOn()){
				playHDButton.setChecked(true);
			}

			if(MyMediaPlayer.getMediaPlayerStatus() == MediaPlayerStatus.BUFFERING) {
				startLoading();
				playButton.setChecked(true);
				textViewLoading.setText("bufferring");
			}

			if(MyMediaPlayer.getMediaPlayerStatus() == MediaPlayerStatus.PLAYING || MyMediaPlayer.getMediaPlayerInstance().isPlaying()) {
				stopLoading();
				playButton.setChecked(true);
				textViewLoading.setText("playing...");
			}

			if(MyMediaPlayer.getMediaPlayerStatus() == MediaPlayerStatus.PAUSED) {
				stopLoading();
				playButton.setChecked(false);
				textViewLoading.setText("paused");
			}

			if(MyMediaPlayer.getMediaPlayerStatus() == MediaPlayerStatus.FORCE_PAUSED) {
				stopLoading();
				playButton.setChecked(false);
				textViewLoading.setText("forcePaused");
			}

			if(MyMediaPlayer.getMediaPlayerStatus() == MediaPlayerStatus.RESETED) {
				stopLoading();
				playButton.setChecked(false);
				textViewLoading.setText("reset");
			}

			if(MyMediaPlayer.getMediaPlayerStatus() == MediaPlayerStatus.RELEASED) {
				stopLoading();
				playButton.setChecked(false);
				textViewLoading.setText("released");
			}
		} catch (Exception e) {
			stopLoading();
			playButton.setChecked(false);
			//MyMediaPlayer.releaseMediaPlayer();
		}
	}

	/*@Override
	protected void onDestroy() {
		super.onDestroy();
	}*/

	private void stopLoading() {
		loading.clearAnimation();
		loading.setVisibility(View.INVISIBLE);
	}

	private void startLoading() {
		loading.startAnimation(rotation);
		loading.setVisibility(View.VISIBLE);
	}


	public void onToggleClicked(View view) {
		try {
			boolean on = ((ToggleButton) view).isChecked();
			if (on) {
				// Play
				MyMediaPlayer.setMediaPlayerPauseClicked(false);
				handler.removeCallbacks(runnable);

				if(MyMediaPlayer.getMediaPlayerStatus() == MediaPlayerStatus.RELEASED || 
						MyMediaPlayer.getMediaPlayerStatus() == MediaPlayerStatus.RESETED){
					startLoading();

					Intent intent3 = new Intent(MyMediaPlayer.SVC_NOTIFICATION);
					intent3.putExtra(MyMediaPlayer.VALUE, MyMediaPlayerServiceNotifyStatus.GET_AUDIO_FOCUS.ordinal());
					sendBroadcast(intent3);
					//MyMediaPlayer.getMediaPlayerAudioFocus(getApplicationContext());

					Intent intent2 = new Intent(MyMediaPlayer.SVC_NOTIFICATION);
					intent2.putExtra(MyMediaPlayer.VALUE, MyMediaPlayerServiceNotifyStatus.PREPARE_PLAYER.ordinal());
					sendBroadcast(intent2);
					//MyMediaPlayer.prepareMediaPlayer();
					textViewLoading.setText("preparing...");
				}else if (MyMediaPlayer.getMediaPlayerStatus() == MediaPlayerStatus.PAUSED){
					Intent intent1 = new Intent(MyMediaPlayer.SVC_NOTIFICATION);
					intent1.putExtra(MyMediaPlayer.VALUE, MyMediaPlayerServiceNotifyStatus.PLAY_PLAYER.ordinal());
					sendBroadcast(intent1);
					//MyMediaPlayer.playMediaPlayer();					
					textViewLoading.setText("playing...");
				}else if (MyMediaPlayer.getMediaPlayerStatus() == MediaPlayerStatus.FORCE_PAUSED) {
					startLoading();

					Intent intent3 = new Intent(MyMediaPlayer.SVC_NOTIFICATION);
					intent3.putExtra(MyMediaPlayer.VALUE, MyMediaPlayerServiceNotifyStatus.GET_AUDIO_FOCUS.ordinal());
					sendBroadcast(intent3);
					//MyMediaPlayer.getMediaPlayerAudioFocus(getApplicationContext());

					Intent intent1 = new Intent(MyMediaPlayer.SVC_NOTIFICATION);
					intent1.putExtra(MyMediaPlayer.VALUE, MyMediaPlayerServiceNotifyStatus.PREPARE_PLAYER.ordinal());
					sendBroadcast(intent1);
					//MyMediaPlayer.prepareMediaPlayer();
					textViewLoading.setText("preparing...");
				}
			} else {
				// Pause
				MyMediaPlayer.setMediaPlayerPauseClicked(true);

				if(MyMediaPlayer.getMediaPlayerInstance().isPlaying()|| MyMediaPlayer.getMediaPlayerStatus() == MediaPlayerStatus.PLAYING){
					Intent intent1 = new Intent(MyMediaPlayer.SVC_NOTIFICATION);
					intent1.putExtra(MyMediaPlayer.VALUE, MyMediaPlayerServiceNotifyStatus.PAUSE_PLAYER.ordinal());
					sendBroadcast(intent1);					
					//MyMediaPlayer.pauseMediaPlayer();
					textViewLoading.setText("paused");

					//handler.removeCallbacks(runnable);
					timerToStopPlayback(timerToStopPlayback);
				}else {
					Intent intent1 = new Intent(MyMediaPlayer.SVC_NOTIFICATION);
					intent1.putExtra(MyMediaPlayer.VALUE, MyMediaPlayerServiceNotifyStatus.RESET_PLAYER.ordinal());
					sendBroadcast(intent1);
					//MyMediaPlayer.resetMediaPlayer();
					MyMediaPlayer.setMediaPlayerStatus(MediaPlayerStatus.FORCE_PAUSED);
					stopLoading();
					textViewLoading.setText("forcePaused");
				}
			}
		}catch (Exception e) {
			//stopLoading();
			//playButton.setChecked(false);
			//Intent intent1 = new Intent(MyMediaPlayer.SVC_NOTIFICATION);
			//intent1.putExtra(MyMediaPlayer.VALUE, MyMediaPlayerServiceNotifyStatus.RELEASE_PLAYER.ordinal());
			//sendBroadcast(intent1);
			//MyMediaPlayer.releaseMediaPlayer();
		}
	}

	public void onToggleHDClicked(View view) {
		try {
			boolean on = ((ToggleButton) view).isChecked();
			if (on) {
				MyMediaPlayer.setIsHDOn(true);
				if (MyMediaPlayer.getMediaPlayerInstance().isPlaying() || MyMediaPlayer.getMediaPlayerStatus() == MediaPlayerStatus.PLAYING){
					startLoading();

					Intent intent3 = new Intent(MyMediaPlayer.SVC_NOTIFICATION);
					intent3.putExtra(MyMediaPlayer.VALUE, MyMediaPlayerServiceNotifyStatus.RESET_N_PREPARE.ordinal());
					sendBroadcast(intent3);
					//MyMediaPlayer.resetAndPrepareMediaPlayer();					
					textViewLoading.setText("hd preparing");
				} else{
					Intent intent2 = new Intent(MyMediaPlayer.SVC_NOTIFICATION);
					intent2.putExtra(MyMediaPlayer.VALUE, MyMediaPlayerServiceNotifyStatus.RESET_PLAYER.ordinal());
					sendBroadcast(intent2);
					textViewLoading.setText("reset");
				}
			}else {
				MyMediaPlayer.setIsHDOn(false);
				if (MyMediaPlayer.getMediaPlayerInstance().isPlaying() || MyMediaPlayer.getMediaPlayerStatus() == MediaPlayerStatus.PLAYING){
					startLoading();

					Intent intent3 = new Intent(MyMediaPlayer.SVC_NOTIFICATION);
					intent3.putExtra(MyMediaPlayer.VALUE, MyMediaPlayerServiceNotifyStatus.RESET_N_PREPARE.ordinal());
					sendBroadcast(intent3);
					//MyMediaPlayer.resetAndPrepareMediaPlayer();				
					textViewLoading.setText("preparing");
				} else{
					Intent intent2 = new Intent(MyMediaPlayer.SVC_NOTIFICATION);
					intent2.putExtra(MyMediaPlayer.VALUE, MyMediaPlayerServiceNotifyStatus.RESET_PLAYER.ordinal());
					sendBroadcast(intent2);
					textViewLoading.setText("reset");
				}
			}
		}catch (Exception e) {
			MyMediaPlayer.setIsHDOn(false);
			stopLoading();
			playHDButton.setChecked(false);

			//Intent intent3 = new Intent(MyMediaPlayer.SVC_NOTIFICATION);
			//intent3.putExtra(MyMediaPlayer.VALUE, MyMediaPlayerServiceNotifyStatus.RELEASE_PLAYER.ordinal()); // value == 6
			//sendBroadcast(intent3);
			//MyMediaPlayer.releaseMediaPlayer();
		}
	}


	private void timerToStopPlayback(int time) {
		//Handler handler = new Handler();
		handler.removeCallbacks(runnable);
		handler.postDelayed(runnable = new Runnable() {
			@Override
			public void run() {
				if (!MyMediaPlayer.getMediaPlayerInstance().isPlaying() && MyMediaPlayer.getMediaPlayerStatus() != MediaPlayerStatus.PLAYING) {

					Intent intent2 = new Intent(MyMediaPlayer.SVC_NOTIFICATION);
					intent2.putExtra(MyMediaPlayer.VALUE, MyMediaPlayerServiceNotifyStatus.RESET_PLAYER.ordinal()); // value == 1
					sendBroadcast(intent2);
					//MyMediaPlayer.resetMediaPlayer();

					playButton.setChecked(false);
					textViewLoading.setText("resetted");
				}
			}
		}, time);
	}


	private void startNotificationOnStatusBar() {
		// Notification show in status bar
		/*NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
				this).setSmallIcon(R.drawable.icon).setContentTitle("TNL Radio").setContentText("content text");
		mBuilder.setPriority(2);

		// Creates an explicit intent for an Activity in your app
		// Intent resultIntent = new Intent(this, MainActivity.class);
		Intent resultIntent = this.getIntent();
		TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
		// Adds the back stack for the Intent (but not the Intent itself)
		stackBuilder.addParentStack(MainActivity.class);
		// Adds the Intent that starts the Activity to the top of the stack
		stackBuilder.addNextIntent(resultIntent);

		PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0,
				PendingIntent.FLAG_UPDATE_CURRENT);
		mBuilder.setContentIntent(resultPendingIntent);
		NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
		int mId = 0;
		// mId allows you to update the notification later on.
		mNotificationManager.notify(mId, mBuilder.build());*/

		Intent intent3 = new Intent(MyMediaPlayer.SVC_NOTIFICATION);
		intent3.putExtra(MyMediaPlayer.VALUE, MyMediaPlayerServiceNotifyStatus.START_NOTIFICATION.ordinal()); // value == 7
		sendBroadcast(intent3);
	}

	private void stopNotificationOnStatusBar() {
		// Notification clear in status bar
		/*mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
		int mId = 0;
		mNotificationManager.cancel(mId);*/

		Intent intent2 = new Intent(MyMediaPlayer.SVC_NOTIFICATION);
		intent2.putExtra(MyMediaPlayer.VALUE, MyMediaPlayerServiceNotifyStatus.STOP_NOTIFICATION.ordinal()); // value == 8
		sendBroadcast(intent2);
	}


	private void myMediaPlayerServiceCallbacks() {
		try {
			br_commonReceiver = new BroadcastReceiver() {
				@Override
				public void onReceive(Context context, Intent intent) {
					int value = 0;
					if (intent == null) 
						value = 0;
					else 
						value = intent.getIntExtra(MyMediaPlayer.VALUE, 0); // 0 value has an issue TODO: check this

					MyMediaPlayerActivityNotifyStatus whichView = MyMediaPlayerActivityNotifyStatus.values()[value];

					switch (whichView) {
					case CONNECTIVITY :
						textViewLoading.setText("no network");
						playButton.setChecked(true);
						startLoading();
						//MyMediaPlayer.prepareMediaPlayer();

						//Intent intent2 = new Intent(MyMediaPlayer.SVC_NOTIFICATION);
						//intent2.putExtra(MyMediaPlayer.VALUE, MyMediaPlayerServiceNotifyStatus.RESET_N_PREPARE.ordinal()); // value == 8
						//sendBroadcast(intent2);
						//MyMediaPlayer.resetAndPrepareMediaPlayer();						
						break;

					case HEADSET_UNMOUNTED :
						if(MyMediaPlayer.getMediaPlayerInstance().isPlaying()){
							playButton.setChecked(false);
							textViewLoading.setText("paused");

							//Intent intent3 = new Intent(MyMediaPlayer.SVC_NOTIFICATION);
							//intent3.putExtra(MyMediaPlayer.VALUE, MyMediaPlayerServiceNotifyStatus.PAUSE_PLAYER.ordinal()); // value == 8
							//sendBroadcast(intent3);
							//MyMediaPlayer.pauseMediaPlayer();
							timerToStopPlayback(timerToStopPlayback);
						}
						break;

					case AUDIO_FOCUS_LOST :
						if(MyMediaPlayer.getMediaPlayerInstance().isPlaying()){
							playButton.setChecked(false);
							textViewLoading.setText("focuslost released");

							//Intent intent3 = new Intent(MyMediaPlayer.SVC_NOTIFICATION);
							//intent3.putExtra(MyMediaPlayer.VALUE, MyMediaPlayerServiceNotifyStatus.PAUSE_PLAYER.ordinal()); // value == 8
							//sendBroadcast(intent3);
							//MyMediaPlayer.pauseMediaPlayer();
							timerToStopPlayback(timerToStopPlayback);
						}
						break;

						// Lost focus for a short time, but we have to stop
						// playback. We don't release the media player because playback
						// is likely to resume
					case AUDIOFOCUS_LOSS_TRANSIENT: // Pause the player
						playButton.setChecked(false);
						textViewLoading.setText("paused");
						timerToStopPlayback(timerToStopPlayback);
						break;

					case PREPARE_PLAYER: // Pause the player
						textViewLoading.setText("preparing");
						playButton.setChecked(true);
						break;

					case PLAY_PLAYER: // Pause the player
						textViewLoading.setText("playing");
						playButton.setChecked(true);
						break;

					case ON_PREPARED :
						stopLoading();
						if(MyMediaPlayer.getMediaPlayerStatus() != MediaPlayerStatus.FORCE_PAUSED){
							textViewLoading.setText("playing...");

							//MyMediaPlayer.playMediaPlayer();	
							playButton.setChecked(true);
						}
						break;

					case ON_COMPLETION :
						boolean isError = intent.getBooleanExtra("isError", false);
						int isError_ErrorCode = intent.getIntExtra("isError_ErrorCode", -1);

						if(isError){
							textViewLoading.setText("on complete - error");
							stopLoading();
							//MyMediaPlayer.resetMediaPlayer();

							switch(isError_ErrorCode){
							case -1004:
								Toast.makeText(getBaseContext(), "MEDIA_ERROR_IO", Toast.LENGTH_LONG).show();
								break;
							case -1007:
								Toast.makeText(getBaseContext(), "MEDIA_ERROR_MALFORMED", Toast.LENGTH_LONG).show();
								break;
							case 200:
								Toast.makeText(getBaseContext(), "MEDIA_ERROR_NOT_VALID_FOR_PROGRESSIVE_PLAYBACK", Toast.LENGTH_LONG).show();
								break;
							case 100:
								Toast.makeText(getBaseContext(), "MEDIA_ERROR_SERVER_DIED", Toast.LENGTH_LONG).show();
								break;
							case -110:
								Toast.makeText(getBaseContext(), "MEDIA_ERROR_TIMED_OUT", Toast.LENGTH_LONG).show();
								break;
							case 1:
								Toast.makeText(getBaseContext(), "MEDIA_ERROR_UNKNOWN", Toast.LENGTH_LONG).show();
								break;
							case -1010:
								Toast.makeText(getBaseContext(), "MEDIA_ERROR_UNSUPPORTED", Toast.LENGTH_LONG).show();
								break;
							}
							playButton.setChecked(false);
						}else {
							textViewLoading.setText("on complete");
							startLoading();
							//MyMediaPlayer.resetAndPrepareMediaPlayer();		
						}
						break;

					case ON_BUFFERING_UPDATE :
						int percent = intent.getIntExtra("percent", -1);
						if (percent == 100) {
							textViewLoading.setText("buffer 100");
							startLoading();
							//MyMediaPlayer.resetAndPrepareMediaPlayer();
						}
						break;

					case ON_INFO :
						int what = intent.getIntExtra("what", -1);
						if (what == MediaPlayer.MEDIA_INFO_BUFFERING_START || what == 703) {
							startLoading();
							textViewLoading.setText("bufferring");
							MyMediaPlayer.setMediaPlayerStatus(MediaPlayerStatus.BUFFERING);
						} else if (what == MediaPlayer.MEDIA_INFO_BUFFERING_END) {		
							textViewLoading.setText("playing...");
							stopLoading();
						}else {
							stopLoading();
						}
						break;

					default:
						Intent intent3 = new Intent(MyMediaPlayer.SVC_NOTIFICATION);
						intent3.putExtra(MyMediaPlayer.VALUE, MyMediaPlayerServiceNotifyStatus.RESET_PLAYER.ordinal());
						sendBroadcast(intent3);
						//MyMediaPlayer.resetMediaPlayer();
						textViewLoading.setText("reseted");
						playButton.setChecked(false);
						break;
					}
				}
			};
			registerReceiver(br_commonReceiver, new IntentFilter(MyMediaPlayer.ACT_NOTIFICATION)); //SVC sending ACTIVITY Notifications
		} catch (Exception e) {
			stopLoading();
			playButton.setChecked(false);
			//MyMediaPlayer.releaseMediaPlayer();			
		}
	}
}